/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "PLJavaConstTypesDouble.h"

using namespace PLSDK;

PLJavaConstTypesDouble::PLJavaConstTypesDouble(u4 high, u4 low) : PLJavaConst(), m_nan(false), m_negInfinity(false), m_posInfinity(false), m_value(0.0) {
    m_type = PLJavaConst::ConstTypesDouble;
    uint64_t longV = ((uint64_t)high << 32) + low;
    if(longV == 0x7ff0000000000000L) {
        m_negInfinity = true;
        return;
    } else if(longV == 0xfff0000000000000L) {
        m_posInfinity = true;
        return;
    } else if((longV >= 0x7ff0000000000001L && longV <= 0x7fffffffffffffffL) || (longV >= 0xfff0000000000001L && longV <= 0xffffffffffffffffL)) {
        m_nan = true;
        return;
    }
    bool neg = ((longV >> 63) == 0) ? false : true;
    uint64_t e = (uint64_t)((longV >> 52) & 0x7ffL);
    uint64_t m = (e == 0) ? (longV & 0xfffffffffffffL) << 1 : (longV & 0xfffffffffffffL) | 0x10000000000000L;
    m_value = (double)(neg ? (m * 2 ^ (e - 1075)) : (-m * 2 ^ (e - 1075)));
}
